<?php

/**
 * RP System
 *
 * @version 1.0
 * @copyright 2004 Ron Planken
 */

class rp {

	
	function get($player_id = 0){
		
		global $dbh;
		
		if ($player_id == 0) {
			$query ='SELECT  category_id
							,user_id
							,sum(rp_gained) - sum(rp_spend) as rp
						FROM raid_points 
					GROUP BY user_id, category_id;
							';
			
			$rp = SelectMultiRpQuery($query, $dbh);
			
		} else {
			$query = "SELECT rp.raid_id
							,rp.category_id
							,sum(rp_gained) - sum(rp_spend) as rp
							,r.date_time
							,rp.description
							,r.description as 'raid_description'
							,e.name as 'event_name'
							,cat.category_short_name
						FROM raid_points rp
					   JOIN raid r ON r.raid_id = rp.raid_id
					   JOIN event e ON e.event_id = r.event_id
					   JOIN raid_points_category cat ON cat.category_id = rp.category_id
					   WHERE player_id = $player_id
					   GROUP BY raid_id
					   ORDER BY r.date_time DESC
					   ;
							";
			
			$rp = SelectMultiQuery($query, $dbh);
		}
		
		
		
		
		return $rp;

	}
	
	function get_history($user_id = 0, $raid_id){
		
		global $dbh;
		
		if ($user_id == 0) {
			$query ="SELECT  category_id
							,user_id
							,rp_gained as gained
							,rp_spend as spend
						FROM raid_points 
					   WHERE raid_id = $raid_id
							";
		} else {
			$query ="SELECT  category_id
							,user_id
							,rp_gained as gained
							,rp_spend as spend
						FROM raid_points 
					   WHERE user_id = $user_id
					     AND raid_id = $raid_id
							";
		}
		
		$rp = SelectMultiRpQuery($query, $dbh);
		
		return $rp;

	}
	
	
	function get_balance($raid_id) {
		
		global $dbh;
		
			$query ="SELECT  r.category_id
							,r.player_id
							,r.user_id
							,sum(r.rp_gained) - sum(r.rp_spend) as rp_balance
							,p.name
    					    ,p.class_id
							,c.name as 'class'
						    ,c.small_picture as 'class_picture'
							,p.team_id
						    ,te.team_image
						FROM raid_points r
						JOIN player p ON r.player_id = p.player_id
						JOIN team te ON te.team_id = p.team_id
					    JOIN class c ON c.class_id = p.class_id
					   WHERE raid_id = $raid_id
					GROUP BY user_id, category_id
					ORDER BY p.team_id DESC, p.class_id ASC;
							";
		
				
		$rp = SelectMultiRpQuery($query, $dbh);
		
		return $rp;
			
	}
/*
							,c.category_short_name
						    ,c.category_long_name
						    ,c.category_image

						    JOIN raid_points_category c ON c.category_id = r.category_id*/
						    
	function get_category($category_id = 0){
		
		global $dbh;
		
		if ($category_id == 0) {
			$query ='SELECT  category_id
							,category_short_name
							,category_long_name
							,category_image
						FROM raid_points_category
					ORDER BY category_id;
							';
		} else {
			$query = "SELECT  category_id
							,category_short_name
							,category_long_name
							,category_image
						FROM raid_points_category
					   WHERE category_id = $category_id;
							";
		}
		
		
		$categories = SelectMultiQuery($query, $dbh);
		
		return $categories;

	}
	
	function save($rp_id = 0, $raid_id, $user_id, $player_id, $admin_pid, $rp_gained, $rp_spend, $category_id, $description, $type, $item_id) {
		
		global $dbh;
		
		$rp_id = secure_int($rp_id);
		$raid_id = secure_int($raid_id);
		$user_id =  secure_int($user_id);
		$player_id = secure_int($player_id);
		$admin_pid =  secure_int($admin_pid);
		$rp_gained = secure_int($rp_gained);
		$rp_spend = secure_int($rp_spend);
		$category_id =  secure_int($category_id);
		$description = secure_string($description);
		$type =  secure_int($type);
		$item_id = secure_int($item_id);
		$item_name = secure_string($item_name);

		
		if ($rp_id == 0) {
			$query ="INSERT INTO raid_points
						   (rp_id
						   ,raid_id
						   ,user_id
						   ,player_id
						   ,admin_pid
						   ,rp_gained
						   ,rp_spend
						   ,category_id
						   ,description
						   ,type
						   ,item_id
						   ,item_name)
					VALUES ($rp_id
						   ,$raid_id
						   ,$user_id
						   ,$player_id
						   ,$admin_pid
						   ,$rp_gained
						   ,$rp_spend
						   ,$category_id
						   ,$description
						   ,$type
						   ,$item_id
						   ,$item_name)
							";
			$status = InsertQuery($query, $dbh);
			
		} else {
			$query ="UPDATE news
						SET subject = $subject
						   ,player_id = $player_id
						   ,date_time_posted = $date_time_posted
						   ,text_body = $text_body
					  WHERE news_id = $news_id
							";
			$status = UpdateQuery($query, $dbh);
		}
		
		return $status;
		
	}
}
?>